import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Layouts 1.1
import QtWebView 1.1

import UiBase 1.0
import Config 1.0
import UiBase.TC 1.0 as TC

import Code 1.0

import Cpp.Request 1.0 as Request

import "qrc:/Qml/Discover/NewsGroup/News" as TCNews

Item {
    id: root
    property int groupId
    property string groupName
    property var news

    property var pending: null
    property bool communicating: Request.FileUploader.Communicating === fileUploader.status
                                 || Request.FileUploader.Communicating === newsManager.status

    visible: tcNewsIndex.visible
    TCNews.Index {
        id:tcNewsIndex
        parent: indexRoot
        anchors.fill: parent

        groupId: root.groupId
        groupName: root.groupName

        listView.delegate: TCNews.News {
            width: parent.width
            height: 120 * Size.ratio
            news: model
            mouseArea.onClicked: root.news = model
        }
        listView.model: ListModel {
            id:listModel
        }
    }

    MouseArea {
        parent: editorRoot
        width: row.width
        height: parent.height
        Rectangle {
            anchors.fill: parent
            color: Color.background
        }

        Row {
            id:row
            height: parent.height
            Grid {
                id: editGrid

                property alias news: root.news

                onNewsChanged: {
                    if (news) {
                        idLabel.text = String(news.id)
                        timeLabel.text = news.time.toString()
                        groupIdLabel.text = String(news.groupId)
                        iconTitleInput.text = String(news.iconTitle)
                        iconColorInput.text = String(news.iconColor)
                        titleInput.text = String(news.title)
                        authorInput.text = String(news.author)
                        contentInput.text = qsTr("downloading article content")
                        urlLabel.text = news.contentUrl

                        newsPreview._webView.url = news.contentUrl;
                        downloader.download(news.contentUrl)
                    } else {
                        idLabel.text = "_"
                        timeLabel.text = "_"
                        groupIdLabel.text = "_"
                        iconTitleInput.text = "_"
                        iconColorInput.text = "_"
                        titleInput.text = "_"
                        authorInput.text = "_"
                        urlLabel.text = "_"
                        contentInput.text = "_"

                        newsPreview._webView.loadHtml("", "")
                    }
                }

                anchors.verticalCenter: parent.verticalCenter

                columns: 2

                Label {
                    text: qsTr("id : ")
                }
                Label {
                    id: idLabel
                    text: "-"
                }

                Label {
                    text: qsTr("time : ")
                }
                Label {
                    id: timeLabel
                    text: "-"
                }

                Label {
                    text: qsTr("groupId : ")
                }
                Label {
                    id: groupIdLabel
                    text: "-"
                }

                Label {
                    text: qsTr("iconTitle : ")
                }
                TextField {
                    id: iconTitleInput
                }

                Label {
                    text: qsTr("iconColor : ")
                }
                TextField {
                    id: iconColorInput
                }

                Label {
                    text: qsTr("title : ")
                }
                TextField {
                    id: titleInput
                }

                Label {
                    text: qsTr("author : ")
                }
                TextField {
                    id: authorInput
                }

                Label {
                    text: qsTr("contentUrl : ")
                }
                Column {
                    TextField {
                        id:urlLabel
                        readOnly: true
                    }
                    TextArea {
                        id: contentInput
                        onEditingFinished: {
                            if(Request.Downloader.Communicating === downloader.status)
                            {
                                print("abort")
                                downloader.abort()
                            }
                            urlLabel.text = qsTr("not upload yet.")
                            newsPreview._webView.loadHtml(text, "http://tcres.oss-cn-shenzhen.aliyuncs.com/")
                        }
                    }
                    ProgressBar {
                        maximumValue: 1
                        minimumValue: 0
                        value: downloader.progress
                    }
                }


                CheckBox {
                    id: addCheckbox
                    text: qsTr("add")
                }
                Button {
                    text: addCheckbox.checked ? qsTr("add") : qsTr("update")
                    enabled: !communicating
                    onClicked: {
                        fileUploader.uploadString(contentInput.text, "html")
                        var op
                        if (addCheckbox.checked)
                            op = function(news){
                                newsManager.add(groupId, news)
                            }
                        else
                            op = newsManager.update
                        pending = {
                            op: op,
                            news: {
                                id: Number(idLabel.text),
                                groupId: Number(groupIdLabel.text),
                                iconTitle: iconTitleInput.text,
                                iconColor: iconColorInput.text,
                                title: titleInput.text,
                                author: authorInput.text
                            }
                        }
                    }
                }
                CheckBox {
                    id:removeCheckbox
                    text: qsTr("remove")
                }
                Button {
                    text: qsTr("remove")
                    enabled: removeCheckbox.checked && !communicating
                    onClicked: {
                        newsManager.remove(Number(idLabel.text))
                    }
                }
            }

            // preview
            TCNews.NewsView {
                id: newsPreview
                width: simulatorWidth
                height: simulatorHeight
                //news: root.news // use _webView.url to control this behavor
            } // end preview
        } // end row
    }

    Request.Downloader {
        id:downloader
        onOk: {
            contentInput.text = text
            //newsPreview._webView.loadHtml(text, "")
        }
    }

    Request.NewsQuery {
        id: newsQuery
        onOk: {
            listModel.clear()
            for (var i in news) {
                listModel.append(news[i])
            }
        }
        Component.onCompleted: {
            queryAll(groupId)
        }
    }
    Request.NewsManager {
        id: newsManager
        onOk: {
            newsQuery.queryAll(groupId)
        }
    }
    Request.FileUploader {
        id: fileUploader
        onOk: {
            var news = pending.news
            news.contentUrl = remotePath
            pending.op(news)
            pending = null
        }
    }
}
